package service

import (
	"duoke/utils/yaml"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
	tts "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tts/v20190823"
)

var TxCloud = new(sTxCloud)

type sTxCloud struct{}

func (s *sTxCloud) TextToVoice(text string, SessionId string) (*string, error) {
	config := yaml.GetConfig()
	credential := common.NewCredential(
		config.TencentCloud.SecretId,
		config.TencentCloud.SecretKey,
	)
	// 实例化一个client选项，可选的，没有特殊需求可以跳过
	cpf := profile.NewClientProfile()
	cpf.HttpProfile.Endpoint = "tts.tencentcloudapi.com"
	// 实例化要请求产品的client对象,clientProfile是可选的
	client, _ := tts.NewClient(credential, "ap-beijing", cpf)
	// 实例化一个请求对象,每个接口都会对应一个request对象
	request := tts.NewTextToVoiceRequest()
	// 音色，朗读者
	request.VoiceType = common.Int64Ptr(config.TencentCloud.VoiceType)
	// 音量
	request.Volume = common.Float64Ptr(config.TencentCloud.Volume)
	// 播放文本
	request.Text = common.StringPtr(text)
	request.SessionId = common.StringPtr(SessionId)
	// 返回的resp是一个TextToVoiceResponse的实例，与请求对象对应
	response, err := client.TextToVoice(request)
	if _, ok := err.(*errors.TencentCloudSDKError); ok {
		return nil, err
	}
	if err != nil {
		return nil, err
	}
	return response.Response.Audio, err
}
